<?php

/**
 * 新建路径
 * @param string $path      目录路径
 * @param string $logFile   目录头
 * @return void
 */
function makeDir(string $path, string $logFile = '/debug_log'): void
{
    $path = str_replace('\\', '/', $path);
    if(count($logFilePath = array_filter((array)explode('/', $logFile))) > 1){
        unset($logFilePath[1]);
        makeDir('/' . implode('/', $logFilePath));
    }else{
        if(!is_dir(LOG_PATH)){
            mkdir(LOG_PATH);
        }
        if(!is_dir(LOG_PATH . $logFile)){
            mkdir(LOG_PATH . $logFile);
        }
    }
    $pathArray = array_filter((array)explode('/', $path));
    if(!empty($pathArray) and count($pathArray) > 1){
        $folderPath = "/" . implode('/', $pathArray);
        $file = '';
        if(!is_dir(LOG_PATH . $logFile . $folderPath)) {
            foreach ($pathArray as $v) {
                $somethingPath = LOG_PATH . $logFile . '/' . $file . $v;
                if (!is_dir($somethingPath)) {
                    mkdir($somethingPath);
                }
                $file .= $v . '/';
            }
        }
    }else{
        is_dir(LOG_PATH . $logFile . $path) or mkdir(LOG_PATH . $logFile . $path);
    }
}

/**
 * 存入日志
 * @param string $path 路径
 * @param string $msg 日志内容
 * @param string $logFile   主文件夹
 * @param string $mode 函数模式
 * @return void
 */
function putLog(string $path, string $msg = '', string $logFile ="/debug_log", string $mode = FILE_APPEND): void
{
    $path = str_replace('\\', '/', $path);
    clearstatcache();
    is_dir(LOG_PATH . $logFile . substr($path, 0, strripos($path, '/'))) or makeDir(substr($path, 0, strripos($path, '/')), $logFile);
    file_put_contents(LOG_PATH . $logFile . $path, '时间：' . time() . '|'.date('Y-m-d H:i:s') . PHP_EOL . $msg . PHP_EOL . PHP_EOL, $mode);
}

/**
 * worker日志
 * @param string $path
 * @param string $msg
 * @param string $logFile
 * @param string $mode
 * @return void
 */
function workerLog(string $path, string $msg, string $logFile ="/debug_log/worker", string $mode = FILE_APPEND): void
{
    putLog($path . date('/Y/m/d') . '.log', $msg, $logFile, $mode);
}

/**
 * worker消息
 * @param string $msg
 * @param string $path
 * @return void
 */
function workerMessage(string $msg, string $path = ''): void
{
    workerLog('/message' . $path, $msg);
}

/**
 * worker错误
 * @param string $msg
 * @param string $path
 * @return void
 */
function workerError(string $msg, string $path = ''): void
{
    workerLog('/error' . $path, $msg);
}

/**
 * 记录报错日志信息
 * @param string $msg
 * @return void
 */
function putErrorLog(string $msg){
    putLog( date('/Ymd') . DIRECTORY_SEPARATOR . 'error.log', $msg, '/error');
}

/**
 * 设置消息体
 * @param string $msg
 * @param string $msgType
 * @param array $msgData
 * @return string
 */
function sendMsg(string $msg, string $msgType = 'warning', array $msgData = []): string
{
    return sendData('onlyMsg', ['msg' => $msg, 'msgType' => $msgType, 'msgData' => $msgData]); //发送提示信息
}


/**
 * 设置数据体
 * @param string $type
 * @param array $mainInfo
 * @return string
 */
function sendData(string $type, array $mainInfo = []): string
{
    return json_encode(['messageType' => $type, 'data' => $mainInfo]);
}

/**
 * 多维数组的倒序排序
 * @param array $arr
 * @return array
 */
function arrayMultiKeyRsort(array $arr): array
{
    $res = [];
    krsort($arr);
    foreach($arr as $k => $v){
        if(is_array($v)){
            $res[$k] = arrayMultiKeyRsort($v);
        }else{
            $res[$k] = $v;
        }
    }
    return $res;
}

function success($msg = ''): array
{
    return ['code' => 200, 'msg' => $msg];
}
function error($msg = '', $code = 500): array
{
    return ['code' => $code, 'msg' => $msg];
}